package com.wwj.feedback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.feedback.constant.ConstantFinal;
import com.wwj.feedback.dao.AnnouncementDao;
import com.wwj.feedback.entitiy.AnnouncementEntity;
import com.wwj.feedback.entitiy.FeedbackQuestionEntity;
import com.wwj.feedback.service.AnnouncementService;
import com.wwj.feedback.to.AnnouncementTo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementDao, AnnouncementEntity> implements AnnouncementService {

    @Resource
    private AnnouncementDao announcementDao;

    /**
     * 查询最近三条的公告信息
     * select * from announcement ORDER BY announce_time DESC LIMIT 3
     */
    @Override
    public List<AnnouncementTo> getAnnouncementTop3() {
        // 数据查询
        QueryWrapper<AnnouncementEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(ConstantFinal.FIELD_ANNOUNCE_TIME).last("limit 3");
        List<AnnouncementEntity> announcementEntities = announcementDao.selectList(queryWrapper);
        // 实体拷贝
        List<AnnouncementTo> announcementToList = new ArrayList<>();
        announcementEntities.forEach(item -> {
            AnnouncementTo announcementTo = new AnnouncementTo();
            BeanUtils.copyProperties(item, announcementTo);
            announcementToList.add(announcementTo);
        });
        return announcementToList;
    }


}
